package com.qf.fmall2302.wxpay;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import org.apache.shiro.crypto.hash.Md5Hash;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

public class TestWxPay {

    public static final String url1 = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    public static final String appkey = "sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC";

    public static void main(String[] args) {
        //1. 组织请求map，必传字段如下
        HashMap<String, String> map = new HashMap<>();
        map.put("appid","wx632c8f211f8122c6");
        map.put("mch_id","1497984412"); //商户号
        String randomstr = UUID.randomUUID().toString().replaceAll("-", "");
        map.put("nonce_str",randomstr); // 长度32个字符的随机字符串，干扰项
        map.put("body","testproduct"); // 订单描述信息

        map.put("out_trade_no","202308241617595"); // 订单编号
        map.put("total_fee","1");  // 1 分钱
        map.put("trade_type","NATIVE"); // 支付类型
        map.put("fee_type","CNY"); // 币种
        map.put("notify_url","http://47.118.45.73:8080/pay/callback"); // 商户提供的回调地址
        map.put("sign_type","MD5"); // 签名算法

        // 签名处理
        //1. 对所有参数按照参数名称进行升序排序
        TreeMap<String, String> treeMap = new TreeMap<>(map);
        System.out.println(treeMap);

        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            builder.append(entry.toString());
            builder.append("&");
        }
        String stringA = builder.toString();
        System.out.println(stringA);

        String tempString = stringA + "key=" + appkey;
        System.out.println(tempString);

        String sign = new Md5Hash(tempString).toHex().toUpperCase();
        System.out.println(sign);
        map.put("sign",sign);

        // map --> xml 字符串
        String xml = XmlUtil.mapToXmlStr(map);

        String result = HttpRequest.post(url1)
                .body(xml)
                .execute()
                .body();

        System.out.println(result);

        // xml str ----> map
        Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
        String url = (String) resultMap.get("code_url");
        System.out.println(url);

    }


    public static void test3(){
        String xml = "<xml>\n" +
                "   <appid>wx2421b1c4370ec43b</appid>\n" +
                "   <attach>支付测试</attach>\n" +
                "   <body>JSAPI支付测试</body>\n" +
                "   <mch_id>10000100</mch_id>\n" +
                "   <detail><![CDATA[{ \"goods_detail\":[ { \"goods_id\":\"iphone6s_16G\", \"wxpay_goods_id\":\"1001\", \"goods_name\":\"iPhone6s 16G\", \"quantity\":1, \"price\":528800, \"goods_category\":\"123456\", \"body\":\"苹果手机\" }, { \"goods_id\":\"iphone6s_32G\", \"wxpay_goods_id\":\"1002\", \"goods_name\":\"iPhone6s 32G\", \"quantity\":1, \"price\":608800, \"goods_category\":\"123789\", \"body\":\"苹果手机\" } ] }]]></detail>\n" +
                "   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>\n" +
                "   <notify_url>https://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>\n" +
                "   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>\n" +
                "   <out_trade_no>1415659990</out_trade_no>\n" +
                "   <spbill_create_ip>14.23.150.211</spbill_create_ip>\n" +
                "   <total_fee>1</total_fee>\n" +
                "   <trade_type>JSAPI</trade_type>\n" +
                "   \n" +
                "</xml>";
        String result2 = HttpRequest.post(url1)
                .body(xml)  // 设置请求体中的数据，字符串可以自定义任何格式
                .execute()  // 发送请求
                .body();    // 获取响应体中的内容

        System.out.println(result2);
    }

    public static void test2()
    {


        //POST请求
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("city", "北京");

        String result1 = HttpUtil.post(url1, paramMap);

        System.out.println(result1);
    }

    public static void test1(){
        //1. 如何发送http请求给指定 url呢
        String url = "https://www.baidu.com";
        //GET请求
        String content = HttpUtil.get(url);

        System.out.println(content);
    }

}
